#include "MovingPathAnimator.h"

using namespace std;

MovingPathAnimator::MovingPathAnimator (void): 
	 	sprite((Sprite*) 0), 
		anim((MovingPathAnimation*) 0), currFrame(0xFF) {}

void MovingPathAnimator::Start(Sprite* s, MovingPathAnimation* a, timestamp_t t) {
	sprite = s;
	anim = a;
	lastTime = t;
	state = ANIMATOR_RUNNING;
	currEntry = anim->GetFirstPathEntry();
	sprite->SetFrame(currFrame = currEntry->frame);
}

void MovingPathAnimator::Progress (timestamp_t currTime) {
	while (currTime > lastTime && currTime - lastTime >= currEntry->delay) {
		//sprite->Move(currEntry->dx, currEntry->dy);
		sprite->SetFlipH(currEntry->h_flip);
		sprite->SetFlipV(currEntry->v_flip);
		sprite->SetFrame(currEntry->frame);
		sprite->Move(currEntry->dx, currEntry->dy);
		lastTime += currEntry->delay;
		++currEntry;
		if (currEntry == anim->GetLastPathEntry()) {
			state = ANIMATOR_FINISHED;
			currEntry = anim->GetFirstPathEntry();
			NotifyStopped();
			return;
	    }
	
	}
}

